#!/bin/bash

set -eo pipefail

dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
root_dir="$(cd $dir/.. && pwd)"
tests_dir="$(cd $root_dir/tests && pwd)"
scenario_dir="$tests_dir/Scenarios"
cache_file="$root_dir/.scencache"
shasumVersion="$(shasum -v)"
inflectionVersion="6.01"

if [ "$(printf '%s\n' "$inflectionVersion" "$shasumVersion" | sort -V | head -n1)" = "$inflectionVersion" ]; then
  checksum="$(echo $root_dir/spec/scenario{,/**}/*.scen | xargs shasum -U | shasum -U | cut -d' ' -f 1)"
else
  checksum="$(echo $root_dir/spec/scenario{,/**}/*.scen | xargs shasum -p | shasum -p | cut -d' ' -f 1)"
fi

if [ -z "$rebuild" -a -f "$cache_file" ]; then
  cached="$(cat $cache_file)"

  if [ "$cached" == "$checksum" ]; then
    echo "Skipping Scenario Stub Rebuild (set rebuild=true to force)"
    exit 0
  fi
fi

echo "Build scenario stubs..."
rm -rf "$scenario_dir" && mkdir "$scenario_dir"

scenario_test="$(cat <<-EOF
const scenario = require('REL_PATHScenario');

scenario.run('SCEN_FILE');
EOF
)"

cd $root_dir/spec/scenario

for scenario in .{,/**}/*.scen; do
  base="${scenario#.\/}"
  filename="${base%.*}ScenTest.js"
  final="$scenario_dir/$filename"
  mkdir -p "$(dirname "$final")"
  rel_path="$(echo "$scenario" | sed 's$[^/]$$g' | sed 's$/$../$g')"
  echo "$scenario_test" | sed "s^SCEN_FILE^$base^g" | sed "s^REL_PATH^$rel_path^g" > "$final"
done

echo "$checksum" > "$cache_file"
